import 'package:flutter/material.dart';

class TaskList extends StatefulWidget {
  final int total;
  final List list;
  final void Function(dynamic item) onclick;
  final void Function() upRefresh;
  const TaskList({Key key, this.total, this.list, this.onclick, this.upRefresh})
      : super(key: key);

  @override
  TaskListState createState() => TaskListState();
}

class TaskListState extends State<TaskList> {
  bool isLoading = true;
  ScrollController _scrollController = new ScrollController();

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        if (widget.list.length < widget.total) {
          widget.upRefresh();
        }
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color.fromRGBO(248, 248, 249, 1),
      padding: EdgeInsets.fromLTRB(16, 20, 16, 0),
      child: ListView.builder(
        controller: _scrollController,
        itemCount: widget.list.length + 1,
        itemBuilder: (BuildContext context, int index) {
          if (widget.list.length == 0) {
            return Center(child: Text('暂无数据'));
          } else if (widget.list.length == index) {
            return _buildProgressIndicator();
          } else {
            return createItem(widget.list[index]);
          }
        },
      ),
    );
  }

  Widget _buildProgressIndicator() {
    return new Padding(
      padding: EdgeInsets.only(bottom: 16),
      child: new Center(
        child: new Opacity(
          opacity: isLoading ? 1.0 : 00,
          // child: new CircularProgressIndicator(),
          child: widget.list.length == widget.total
              ? Text('已加载全部')
              : Text('加载中...'),
        ),
      ),
    );
  }

  // 生成单个菜单项
  Widget createItem(item) {
    return GestureDetector(
      onTap: () {
        widget.onclick(item);
      },
      child: Container(
        width: double.infinity,
        height: 84,
        margin: EdgeInsets.only(
          bottom: 12,
        ),
        padding: EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: Color.fromRGBO(255, 255, 255, 1),
          border: Border.all(
            width: 1,
            color: Color.fromRGBO(229, 229, 229, 1),
          ),
          borderRadius: BorderRadius.circular(3),
        ),
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                item.hasInputAuth
                    ? Image(
                        image: AssetImage('assets/images/shangchuan.png'),
                        width: 14,
                        height: 14,
                      )
                    : SizedBox(),
                Container(
                  alignment: Alignment.centerLeft,
                  margin: EdgeInsets.only(left: item.hasInputAuth ? 4 : 0),
                  child: Text(
                    item.code ?? '--',
                    style: TextStyle(
                      fontSize: 14,
                      height: 20 / 14,
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    alignment: Alignment.centerRight,
                    margin: EdgeInsets.only(right: 6),
                    child: Text(
                      '检验数量:',
                      style: TextStyle(
                        fontSize: 12,
                        height: 16.5 / 12,
                      ),
                    ),
                  ),
                ),
                Text(
                  (item.finishQuantity == null
                          ? '0'
                          : item.finishQuantity.toString()) +
                      '/' +
                      item.checkQuantity.toString(),
                  style: TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.w600,
                    height: 18 / 15,
                    color: Color.fromRGBO(89, 131, 252, 1),
                  ),
                ),
              ],
            ),
            Expanded(
              flex: 1,
              child: Container(
                child: Row(
                  children: <Widget>[
                    Container(
                      alignment: Alignment.bottomLeft,
                      child: Text(
                        item.customerName ?? '--',
                        style: TextStyle(
                          fontSize: 12,
                          height: 16 / 12,
                          color: Color.fromRGBO(150, 151, 154, 1),
                        ),
                      ),
                    ),
                    Expanded(
                      child: Container(
                        alignment: Alignment.bottomRight,
                        child: Text(
                          item.createTime ?? '--',
                          style: TextStyle(
                            fontSize: 12,
                            height: 16 / 12,
                            color: Color.fromRGBO(150, 151, 154, 1),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
